package com.example.bzh.gamecat.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SwitchCompat;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bzh.gamecat.R;
import com.example.bzh.gamecat.bean.UserBean;
import com.example.bzh.gamecat.dao.UserBeanDao;
import com.example.bzh.gamecat.ui.MainActivity;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;

public class LoginActivity extends AppCompatActivity {

    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    @BindView(R.id.login_num_image)
    ImageView loginNumImage;
    @BindView(R.id.login_num)
    EditText loginNum;
    @BindView(R.id.login_num_clear)
    ImageView loginNumClear;
    @BindView(R.id.login_pwd_image)
    ImageView loginPwdImage;
    @BindView(R.id.login_pwd)
    EditText loginPwd;
    @BindView(R.id.login_pwd_open)
    ImageView loginPwdOpen;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.iv_qq)
    ImageView ivQq;
    @BindView(R.id.iv_weinxin)
    ImageView ivWeinxin;
    @BindView(R.id.iv_weibo)
    ImageView ivWeibo;
    private Unbinder unbinder;
    private String phone;
    private String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        unbinder = ButterKnife.bind(this);

        ShareSDK.initSDK(this);

    }

    @OnClick({R.id.btn_login,R.id.tv_register,R.id.iv_qq})
    public void  onLoginItemClick(View view){
        switch (view.getId()){
            case R.id.tv_register:
                startActivity(new Intent(this,RegisterActivity.class));
                break;
            case R.id.btn_login:
                phone = loginNum.getText().toString().trim();
                password = loginPwd.getText().toString().trim();
                UserBean userBean = new UserBean();
                userBean.setPhone(phone);
                userBean.setPassword(password);
                boolean checkLogin = UserBeanDao.checkLogin(this, userBean);
                if (checkLogin){
                    startActivity(new Intent(this,MainActivity.class));
                }else {
                    Toast.makeText(this,"账户或密码错误，请重新输入",Toast.LENGTH_SHORT).show();
                    loginNum.setText("");
                    loginPwd.setText("");
                }
                break;
            case R.id.iv_qq:
                Platform pf = ShareSDK.getPlatform(this,QQ.NAME);
                //删除授权信息
                if (pf.isAuthValid()){
                    pf.removeAccount();
                }
                platformLogin(pf);

                break;
        }
    }



    /**
     * 使用第三方平台的授权登录
     * */
    private void platformLogin(final Platform plat) {
        // 判断指定平台是否已经完成授权
        if (plat.isAuthValid()) {
            String userId = plat.getDb().getUserId();
            if (userId != null) {
                Toast.makeText(LoginActivity.this, "使用第三方平台登录成功", Toast.LENGTH_SHORT).show();
            }
        }else{
            Toast.makeText(LoginActivity.this, "未获得第三方平台登录授权", Toast.LENGTH_SHORT).show();
            plat.authorize();
        }
        plat.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onComplete(Platform platform, int action, HashMap<String, Object> res) {
               // Log.e("temp", "onComplete:msg="+res);
                if (action == Platform.ACTION_USER_INFOR) {
                    PlatformDb platDB = platform.getDb();//获取数平台数据DB
                    //通过DB获取各种数据
                    platDB.getToken();
                    platDB.getUserGender();
                    platDB.getUserIcon();
                    platDB.getUserId();
                    platDB.getUserName();

                    Log.e("temp"," platDB.getUserGender();="+ platDB.getUserGender());
                    Log.e("temp", "getUserName="+platDB.getUserName());
                    Log.e("temp", "getUserIcon="+platDB.getUserIcon());

                    startActivity(new Intent(LoginActivity.this,MainActivity.class));
                }

            }

            @Override
            public void onCancel(Platform arg0, int arg1) {
                // TODO Auto-generated method stub

            }
        });
        plat.showUser(null);

    }

    /**'
     * 回调获得第三方数据
     *
     */
    public void onComplete(Platform platform, int action, HashMap<String, Object> res) {
        if (action == Platform.ACTION_USER_INFOR) {
            PlatformDb platDB = platform.getDb();//获取数平台数据DB
            //通过DB获取各种数据
            platDB.getToken();
            platDB.getUserGender();
            platDB.getUserIcon();
            platDB.getUserId();
            platDB.getUserName();
        }
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }
}
